from django.shortcuts import render

from .models import User
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.generics import CreateAPIView
from .serializers import createUserSerializer


class UserView(CreateAPIView):
    """
    用户注册：新增用户
    CreateAPIView  # 继承自： GenericAPIView、CreateModelMixin
    """
    # 指定序列化器
    serializer_class = createUserSerializer


class userNameCountAPI(APIView):
    """
    获取当前用户名数量 传入前端，通过前端判断当前注册/登录用户是否存在
    """

    def get(self, request, username):
        count = User.objects.filter(username=username).count()

        data = {
            'username': username,
            'count': count,
        }
        return Response(data)


class userEmailCountAPI(APIView):
    """
    获取当前邮箱数量  传给前端，判断是否注册过（是否存在数据库）
    """

    def get(self, request, email):
        count = User.objects.filter(email=email).count()

        data = {
            'email': email,
            'count': count,
        }

        return Response(data)
